--[[----------------------------------------------------------------------------
HighlightLinks v1.5.3
Author: VladVRO, mozers

- Highlight links in the text (automatic or menu command)
- Select all link by double clicking on it
- Open link in a browser by double clicking with the SHIFT key held Ctrl.

Warning:
The script only works on SciTE-Ru.
The script uses features of COMMON.lua (EditorMarkText, EditorClearMarks)
and external library shell (shell.exec)
-----------------------------------------------

connection:
Add to SciTEStartup.lua line:
   dofile (props ["SciteDefaultHome"] .. "\ \ tools \ \ HighlightLinks.lua")
Set-up:
Create a menu item:
   command.name.137. * = Highlight Links
   command.137. * = HighlightLinks
   command.mode.137. * = subsystem: lua, savebefore: no
Ask a marker style to highlight links:
   indic.style.13 = # 0000FF, plain
Specify which files you open and save the file will be automatically
run lights:
a list of names separated by commas lexer (lexer for files without a name null)
   highlight.links.lexers = null
or a list of file extensions, separated by commas:
   highlight.links.exts = txt, htm

Includes a setting to change the search mask records:
   highlight.links.mask = https * :/ / [\ w_ &%?. \ - @ $ + = * ~ /] +
-]] ---------------------------------------------- ---

require "shell"

local mark_number = 13
local default_link_mask = "https*://[^ \t\r\n\"\']+"

function HighlightLinks()
	local link_mask = props['highlight.links.mask']
	if link_mask == '' then link_mask = default_link_mask end
	EditorClearMarks(mark_number)
	local flag = SCFIND_REGEXP
	local s,e = editor:findtext(link_mask, flag, 0)
	while s do
		EditorMarkText(s, e-s, mark_number)
		s,e = editor:findtext(link_mask, flag, e+1)
	end
end

local function select_highlighted_link()
	local p = editor.CurrentPos
	if scite.SendEditor(SCI_INDICATORVALUEAT, mark_number, p) == 1 then
		local s = scite.SendEditor(SCI_INDICATORSTART, mark_number, p)
		local e = scite.SendEditor(SCI_INDICATOREND, mark_number, p)
		if s and e then
			editor:SetSel(s,e)
			return true
		end
	end
	return false
end
AddEventHandler("OnDoubleClick", function(shift, ctrl, alt)
	if editor.Focus then
		if select_highlighted_link() and ctrl then
			local url = editor:GetSelText()
			shell.exec(url)
		end
	end
end)

local function auto_highlight()
	local list_lexers = props['highlight.links.lexers']
	local list_exts = props['highlight.links.exts']
	if (list_lexers ~= '' and string.find(','..list_lexers..',', ','..props['Language']..',')) or
	   (list_exts ~= '' and string.find(','..list_exts..',', ','..props['FileExt']..','))
	then
		HighlightLinks()
	end
end
AddEventHandler("OnOpen", auto_highlight)
AddEventHandler("OnSave", auto_highlight)
